package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.ao.RechargeActivityAO;
import com.ruoyi.business.service.RechargeActivityService;
import com.ruoyi.business.vo.RechargeActivityListVO;
import com.ruoyi.common.common.CommonEnum;
import com.ruoyi.common.common.ResultBody;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title: RechargeActivityController
 * @copyright: copyright (c) 2021
 * @author: jiangguanjun
 * @date: 2023/2/12 10:31
 * @firstReview:
 * @lastReview:
 * @desc: 充值活动相关
 */
@Api(value = "RechargeActivityController", tags = "充值活动相关,买m增n")
@RestController
@Slf4j
@RequestMapping("/rechargeActivity")
public class RechargeActivityController {

    @Autowired
    private RechargeActivityService rechargeActivityService ;
    @CrossOrigin
    @ApiOperation(value = "添加充值活动")
    @PostMapping("/add")
    public ResultBody add(@RequestBody RechargeActivityAO rechargeActivityAO){

        log.info("rechargeActivityAO:::::::{}",rechargeActivityAO);
        checkParam(rechargeActivityAO);

        return rechargeActivityService.add(rechargeActivityAO) ;
    }


    @CrossOrigin
    @ApiOperation(value = "列表，获取管理员创建的活动的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uId", value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(name = "status", value = "状态，0：未开始，1：进行中，2：已经结束", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "页长", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer")
    })
    @GetMapping("/listByAdmin")
    public ResultBody<IPage<RechargeActivityListVO>> list(@RequestParam(value = "uId" ,required = false) Integer uId, @RequestParam(value = "status" ,required = false) Integer status){

        log.info("uId:::::::{},status:::{}",uId,status);

        return rechargeActivityService.list(uId,status) ;
    }

    @CrossOrigin
    @ApiOperation(value = "获取当前用户的活动")
    @GetMapping("/listByUser")
    public ResultBody<List<RechargeActivityListVO>> listByUser(){
        Long userId = SecurityUtils.getUserId() ;

        return rechargeActivityService.listByUser(userId) ;
    }

    private void checkParam(RechargeActivityAO rechargeActivityAO) {
        if(rechargeActivityAO.getGiveMoney() == null || rechargeActivityAO.getGiveMoney() == 0){
            throw new BizException(CommonEnum.RECHARGE_ACTIVITY_GIVE_ERROR) ;
        }
        if(rechargeActivityAO.getMoney() == null || rechargeActivityAO.getMoney() == 0 ){
            throw new BizException(CommonEnum.RECHARGE_ACTIVITY_MONEY_ERROR) ;
        }
        if(StringUtils.isBlank(rechargeActivityAO.getName())){
            throw new BizException(CommonEnum.RECHARGE_ACTIVITY_NAME_ERROR) ;
        }
        if(rechargeActivityAO.getStartTime() == null || rechargeActivityAO.getEndTime() == null){
            throw new BizException(CommonEnum.RECHARGE_ACTIVITY_TIME_ERROR) ;
        }
        if(rechargeActivityAO.getEndTime().compareTo(System.currentTimeMillis())<0){
            throw new BizException(CommonEnum.RECHARGE_ACTIVITY_TIME_INVILD_ERROR) ;
        }

    }




}
